package com.mercury.logs.manager;

import cn.hutool.core.util.URLUtil;
import com.mercury.logs.entity.MercuryLogEntity;
import com.mercury.logs.enums.MercuryLogTypeEnum;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * 系统日志工具类
 *
 * @author mercury
 */
@UtilityClass
public class MercuryLogUtils {

    public MercuryLogEntity getSysLog() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects
                .requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        MercuryLogEntity sysLog = new MercuryLogEntity();
        sysLog.setType(MercuryLogTypeEnum.NORMAL.getType());
        sysLog.setRequestUri(URLUtil.getPath(request.getRequestURI()));
        sysLog.setMethod(request.getMethod());
        sysLog.setUserAgent(request.getHeader(HttpHeaders.USER_AGENT));
        return sysLog;
    }
}
